package com.baseweb.webadmin.controller.web;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.constants.AdPositionType;
import com.baseweb.webadmin.core.constants.AdPositionTypeNow;
import com.baseweb.webadmin.core.constants.ChannelType;
import com.baseweb.webadmin.core.model.bvo.ByAdvObjVo;
import com.baseweb.webadmin.core.model.pojo.ByAd;
import com.baseweb.webadmin.core.model.pojo.ByChannel;
import com.baseweb.webadmin.service.ContentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dengsy on 2017-06-22.
 * 主页面
 */
@Controller
public class IndexWebController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(MainWebController.class);
    private final static String SESSION_CHECK_CODE = "checkCode";

    @Autowired
    private ContentService contentService;

    /**
     * 主页面顶部大图轮播查询
     */
    @RequestMapping(value = "/getCenterLunboList", method = RequestMethod.GET)
    public String getCenterLunboList(Model model) {
        List<ByAd> centerLunboList = contentService.findAllByAdByPositionId(AdPositionTypeNow.INDEX_BIGADLUNBO.getCode(),3,"");
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerLunboList != null && centerLunboList.size() > 0) {
            for (ByAd ad : centerLunboList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,null));
                advObjVo.add(v);
            }
        }
        model.addAttribute("centerLunboList", advObjVo);
        return "";
    }

    /**
     * 主页面顶部小图轮播查询
     */
    @RequestMapping(value = "/getCenterBottomList", method = RequestMethod.GET)
    public String getCenterBottomList(Model model) {
        List<ByAd> centerBottomList = contentService.findAllByAdByPositionId(AdPositionTypeNow.LIST_ADSMALL.getCode(),4,"");
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerBottomList != null && centerBottomList.size() > 0) {
            for (ByAd ad : centerBottomList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,null));
                advObjVo.add(v);
            }
        }
        model.addAttribute("centerBottomList", advObjVo);
        return "";
    }

    /**
     * 主页面顶部猜你喜欢查询
     */
    @RequestMapping(value = "/getYouLikeTOP", method = RequestMethod.GET)
    public String getYouLikeTOP(Model model) {
        List<ByAd> youLikeList = contentService.findAllByAdByPositionId(AdPositionTypeNow.CURRENT_GUESS.getCode(),15,"");
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (youLikeList != null && youLikeList.size() > 0) {
            for (ByAd ad : youLikeList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,null));
                advObjVo.add(v);
            }
        }
        model.addAttribute("youLikeListT", advObjVo);
        return "";
    }

    /**
     * 主页面婚礼预约查询
     */
    @RequestMapping(value = "/getHunliyuyueList", method = RequestMethod.GET)
    public String getHunliyuyueList(Model model,String city) {
        List<ByAd> hunliyuyueList = contentService.findAllByAdByPositionId(AdPositionTypeNow.CLASSIC_WEDDING.getCode(), 8,city);
        List<ByAd> hunliyuyueggList = contentService.findAllByAdByPositionId(AdPositionTypeNow.CLASSIC_WEDDINGRIGHT.getCode(),1,city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        List<ByAdvObjVo> advGGObjVo = new ArrayList<ByAdvObjVo>();
        if (hunliyuyueList != null && hunliyuyueList.size() > 0) {
            for (ByAd ad : hunliyuyueList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,city));
                advObjVo.add(v);
            }
        }
        if (hunliyuyueggList != null && hunliyuyueggList.size() > 0) {
            for (ByAd ad : hunliyuyueggList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,city));
                advGGObjVo.add(v);
            }
        }
        model.addAttribute("hunliyuyueList", advObjVo);
        model.addAttribute("hunliyuyueggList", advGGObjVo);
        return "";
    }

    /**
     * 主页特别推荐查询
     */
    @RequestMapping(value = "/getCurList", method = RequestMethod.GET)
    public String getCurList(Model model,String city) {
        List<ByAd> curList = contentService.findAllByAdByPositionId(AdPositionTypeNow.INDEX_TEBIETUIJIAN.getCode(),10,city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (curList != null && curList.size() > 0) {
            for (ByAd ad : curList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 1000,city));
                advObjVo.add(v);
            }
        }
        model.addAttribute("curList", advObjVo);
        return "";
    }

    /**
     * 主页策划师查询
     */
    @RequestMapping(value = "/getCehuaList", method = RequestMethod.GET)
    public String getCehuaList(Model model ,String city) {
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(8, 3,city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6,city));
                advObjVo.add(v);
            }
        }
        List<ByAd> cehuaLList = contentService.findAllByAdByPrarentId(7, 3,city);
        List<ByAd> cehuaRList = contentService.findAllByAdByPrarentId(11, 5,city);
        model.addAttribute("cehuaLList", cehuaLList);
        model.addAttribute("centerList", advObjVo);
        model.addAttribute("cehuaRList", cehuaRList);
        return "";
    }

    /**
     * 主页主持查询
     */
    @RequestMapping(value = "/getZhuchiList", method = RequestMethod.GET)
    public String getZhuchiList(Model model,String city) {
        List<ByAd> zhuchiLList = contentService.findAllByAdByPrarentId(12, 3, city);
        List<ByAd> zhuchiRList = contentService.findAllByAdByPrarentId(16, 5, city);
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(13, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        model.addAttribute("zhuchiLList", zhuchiLList);
        model.addAttribute("zhuchiRList", zhuchiRList);
        model.addAttribute("centerList", advObjVo);
        return "";
    }

    /**
     * 主页摄影师查询
     */
    @RequestMapping(value = "/getSysList", method = RequestMethod.GET)
    public String getSysList(Model model, String city) {
        List<ByAd> sysLList = contentService.findAllByAdByPrarentId(17, 3, city);
        List<ByAd> sysRList = contentService.findAllByAdByPrarentId(21, 3, city);
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(18, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        model.addAttribute("sysLList", sysLList);
        model.addAttribute("sysRList", sysRList);
        model.addAttribute("centerList", advObjVo);
        return "";
    }

    /**
     * 主页化妆师查询
     */
    @RequestMapping(value = "/getHzsList", method = RequestMethod.GET)
    public String getHzsList(Model model, String city) {
        List<ByChannel> list = new ArrayList<ByChannel>();
        List<ByAd> hzsLList = contentService.findAllByAdByPrarentId(27,3, city);
        List<ByAd> hzsRList = contentService.findAllByAdByPrarentId(31, 3, city);
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(28, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        model.addAttribute("hzsLList", hzsLList);
        model.addAttribute("centerList", advObjVo);
        model.addAttribute("hzsRList", hzsRList);
        return "";
    }

    /**
     * 主页摄像师查询
     */
    @RequestMapping(value = "/getSxsList", method = RequestMethod.GET)
    public String getSxsList(Model model,String city) {
        List<ByAd> sxsLList = contentService.findAllByAdByPrarentId(22, 3, city);
        List<ByAd> sxsRList = contentService.findAllByAdByPrarentId(26, 3, city);
        List<ByAd> centerList = contentService.findAllByAdByPrarentId(23, 3, city);
        List<ByAdvObjVo> advObjVo = new ArrayList<ByAdvObjVo>();
        if (centerList != null && centerList.size() > 0) {
            for (ByAd ad : centerList) {
                ByAdvObjVo v = new ByAdvObjVo();
                v.setAd(ad);
                v.setChildren(contentService.findAllByAdByPrarentId(ad.getId(), 6, city));
                advObjVo.add(v);
            }
        }
        model.addAttribute("sxsLList", sxsLList);
        model.addAttribute("sxsRList", sxsRList);
        model.addAttribute("centerList", advObjVo);
        return "";
    }

    /**
     * 主页面低部猜你喜欢查询
     */
    @RequestMapping(value = "/getYouLikeBOTTOM", method = RequestMethod.GET)
    public String getYouLikeBOTTOM(Model model) {

        List<ByAd> youLikeList = contentService.findAllByAdByPositionId(AdPositionType.INDEX_YOULIKEBOTTOM.getCode(),10,"");
        model.addAttribute("youLikeListB", youLikeList);
        return "";
    }

    //公告方法封装Json结构
    public List<ByChannel> setByChannelList(List<ByAd> byAdList, List<ByChannel> byChannelList) {
        List<ByChannel> list = new ArrayList<ByChannel>();
        for (ByChannel byChannel : byChannelList) {
            List<ByAd> byAdLists = new ArrayList<ByAd>();
            for (ByAd byAd : byAdList) {
                if (byChannel.getId() == byAd.getParentId()) {
                    byAdLists.add(byAd);
                }
                byChannel.setByAdList(byAdLists);
            }
            list.add(byChannel);
        }
        return list;
    }

}
